package com.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.springboot.common.BaseContext;
import com.springboot.common.R;
import com.springboot.pojo.ShoppingCart;
import com.springboot.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

        //根据线程绑定获取用户id
        Long userId = BaseContext.getUserId();
        shoppingCart.setUserId(userId);

        //查询当前菜品是否在购物车中
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper=new LambdaQueryWrapper<>();

        if (dishId!= null){
            //当前购物车为菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            //当前购物车为套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询当前菜品是否在购物车中
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if (one!= null){
            //当前菜品在购物车中，数量增加
            Integer number = one.getNumber();
               one.setNumber(number+1);
               shoppingCartService.updateById(one);
        }else{
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        return R.success(one);
    }

    /**
     * 查询购物车列表
     * @return
     */
    @GetMapping("/list")
    public  R<List<ShoppingCart>> list(){
        //根据线程绑定获取用户id
        Long userId = BaseContext.getUserId();
        LambdaQueryWrapper<ShoppingCart> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        wrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public  R<String> clean(){
    LambdaQueryWrapper<ShoppingCart> wrapper=new LambdaQueryWrapper<>();
    wrapper.eq(ShoppingCart::getUserId,BaseContext.getUserId());
    shoppingCartService.remove(wrapper);
      return R.success("清空购物车成功");
    }

    /**
     * 减少购物车数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public  R<String> sub(@RequestBody ShoppingCart shoppingCart){
        Long userId = BaseContext.getUserId();
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper=new LambdaQueryWrapper<>();
        if (dishId!= null){
            //当前购物车为菜品
            wrapper.eq(ShoppingCart::getUserId,userId);
            wrapper.eq(ShoppingCart::getDishId,dishId);
            ShoppingCart byId01 = shoppingCartService.getOne(wrapper);
             byId01.setNumber(byId01.getNumber()-1);
             if(byId01.getNumber()>0){
                 shoppingCartService.updateById(byId01);
             }else {
                 shoppingCartService.remove(wrapper);
             }
        }else {
            //当前减少为套餐
            Long setmealId = shoppingCart.getSetmealId();
            wrapper.eq(ShoppingCart::getUserId,userId);
            wrapper.eq(ShoppingCart::getSetmealId,setmealId);
            ShoppingCart byId02 = shoppingCartService.getOne(wrapper);
            byId02.setNumber(byId02.getNumber()-1);
            if(byId02.getNumber()>0){
                shoppingCartService.updateById(byId02);
            }else {
                shoppingCartService.remove(wrapper);
            }
        }
        return R.success("减少成功");
    }
}
